package com.cll.pmis.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author chenliangliang
 * @date 2018/7/1
 */
public class CommonUtil {

    private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

    /**
     * 正则表达式：验证手机号
     */
    public static final String REGEX_PHONE = "^(((13[0-9])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$";

    public static String timeStamp() {
        return LocalDateTime.now().format(formatter);
    }


    public static String formatDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static Date parseDate(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date parseDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(date);
        } catch (Exception e) {
            return null;
        }
    }


    public static boolean match(String text, String regex) {
        return Pattern.matches(regex, text);
    }


    public static boolean isNumber(String text) {
        return match(text, "\\d+");
    }

    public static boolean isPhone(String text) {
        boolean flag = false;
        try {
            Pattern regex = Pattern.compile(REGEX_PHONE);
            Matcher matcher = regex.matcher(text);
            flag = matcher.matches();
        } catch (Exception e) {
            //
        }
        return flag;
    }


    public static void main(String[] args) {
        System.out.println(isPhone("18756965579"));
        System.out.println(isNumber("577 5"));
        System.out.println(parseDate("2018-08-08 12:06:12","yyyy-MM-dd HH:mm:ss"));
    }
}
